package com.dev.stream;

import com.dev.stream.pojo.Person;
import org.junit.Test;

import java.util.*;

/**
 * @author 戴 骥
 * @date 2021-09-05 17:28
 */
public class PolymericStream {
    @Test
    public void polymericStream() {
        //1 . 获取String集合中最长的元素
        List<String> list = Arrays.asList("李磊", "韩梅梅", "Lucy", "Jim");
        //实现方法1-接口实现
        Optional<String> max = list.stream().max((t1, t2) -> t1.length() - t2.length());
        System.out.println("max = " + max);
        //实现方法2(精简写法)
        Optional<String> max1 = list.stream().max(Comparator.comparingInt(String::length));
        System.out.println("max1 = " + max1);
        //2. 获取Integer集合中的最大值
        List<Integer> list2 = Arrays.asList(7, 6, 9, 4, 11, 6);
        Optional<Integer> max3 = list2.stream().max(Integer::compareTo);
        System.out.println("max3 = " + max3);
        // 自然排序最大值
        Integer max2 = list2.stream().max(Integer::compareTo).get();
        System.out.println("max2 = " + max2);
        // 自定义排序用最大值方法取最小值
        Integer minNum = list2.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        }).get();
        System.out.println("maxNum = " + minNum);
        //找最小
        Integer minInt = list2.stream().min(Integer::compareTo).get();
        System.out.println("minInt = " + minInt);
        //普通计数
        long count = list2.stream().count();
        //记数 找出大于9的个元素个数
        long count1 = list2.stream().filter(t -> t > 9).count();
        System.out.println("count1 = " + count1);
        System.out.println("count = " + count);

        //3. 获取员工工资最高的人
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("韩梅梅", 8900, 22, "女", "武汉"));
        personList.add(new Person("李磊", 7000, 33, "男", "上海"));
        personList.add(new Person("Lucy", 7800, 44, "女", "北京"));
        personList.add(new Person("狗哥", 8200, 55, "男", "深圳"));
        personList.add(new Person("Lily", 9500, 66, "女", "深圳"));
        personList.add(new Person("Jim", 7900, 77, "男", "深圳"));

        //接口方法
        Person salaryPersonMax = personList.stream().max(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getSalary() - o2.getSalary();
            }
        }).get();
        System.out.println("salaryPersonMax = " + salaryPersonMax);
        //简写
        Optional<Person> salaryPersonMax2 = personList.stream().max(Comparator.comparingInt(Person::getSalary));
        System.out.println("salaryPersonMax2 = " + salaryPersonMax2);

        //4 . 计算Integer集合中大于6的元素的个数
        List<Integer> list4 = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
        long count2 = list4.stream().filter(t -> t > 8).count();
        System.out.println("count2 = " + count2);

    }
}
